package com.loong.ems.controller;

import com.loong.ems.domain.entity.SysDept;
import com.loong.ems.domain.vo.resp.NameValueResult;
import com.loong.ems.domain.vo.resp.Result;
import com.loong.ems.domain.vo.resp.SysDeptDetail;
import com.loong.ems.service.SysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 龙勇-229970722
 * @date 2025-01-03 8:52
 * @description: 部门管理，只允许超级管理员管理
 */

@RestController
@RequestMapping("/dept")
@Tag(name = "部门管理")
@Log4j2
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 部门列表
     * @return
     */
    @Operation(summary = "部门列表")
    @GetMapping("/list")
    public Result<List<SysDeptDetail>> list() {
        return Result.success(sysDeptService.listDept());
    }

    /**
     * 修改部门
     * @param sysDept
     * @return
     */
    @Operation(summary = "修改部门")
    @PostMapping("/update")
    public Result update(SysDept sysDept){
        sysDeptService.updateDept(sysDept);
        return Result.success();
    }
    /**
     * 新增部门
     * @param sysDept
     * @return
    */
    @Operation(summary = "新增部门")
    @PostMapping("/add")
    public Result<String> add(@RequestBody SysDept sysDept){
        int i = sysDeptService.addDept(sysDept);
        if(i == 0){
            return Result.error("新增失败");
        }
        return Result.success("新增成功");
    }
    /**
     * 删除部门
     * @param id
     * @return
     */
    @Operation(summary = "删除部门")
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable(value = "id") Long id) {
        int i = sysDeptService.deleteDept(id);
        if (i == 0) {
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }

    /**
     * 部门人数统计
     * @return
     */
    @Operation(summary = "部门人数统计")
    @GetMapping("/count")
    public Result<List<NameValueResult>> count() {
        return Result.success(sysDeptService.count());
    }
}
